package cn.ronghuanet.org.controller;

import cn.ronghuanet.auth.annotation.RonghuanetPermission;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RonghuanetPermission(name = "部门管理",desc="部门的增删改查")
@RestController
@RequestMapping("/department")
@Api(value = "部门管理",description = "部门管理的CRUD")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    /**
     * 查询单个
     * // 返回值 方案一 返回一个 字符串
     *          方案二 返回一个 map
     *          方案三 返回一个自定义的对象 结果集对象 AjaxResult
     */
    @GetMapping("/{id}")
    @RonghuanetPermission(name = "查询指定部门")
    @ApiOperation(value = "查询指定部门")
    public AjaxResult selectOne(@ApiParam(value = "部门id")@PathVariable("id") Long id){
        try {
            Department department = service.selectById(id);
            return  AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 查询全部
     */
    @GetMapping
    @RonghuanetPermission(name = "查询所有部门")
    @ApiOperation(value = "查询全部部门")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = service.selectAll();
            return  AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @RonghuanetPermission(name = "删除指定部门")
    @ApiOperation(value = "删除指定部门")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
     * 保存/修改
     * put/post 请求接受的参数是对象 一定要加上一个注解
     * @RequestBody 我们需要从请求体中获取数据
     */
    @PostMapping
    @RonghuanetPermission(name = "新增或修改部门")
    @ApiOperation(value = "部门新增/修改")
    public AjaxResult saveOrUpdate(@RequestBody Department department){

        try {
            if(Objects.isNull(department.getId())){
                service.insert(department);
            }else{
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 部门列表
     * @return
     * post 和put请求  如果是对象取值 需要 加上注解
     */
    @PutMapping
    @RonghuanetPermission(name = "部门分页查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> list = service.pageList(departmentQuery);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }


    }
    @PatchMapping
    @RonghuanetPermission(name = "部门批量删除")
    public AjaxResult batchDelele(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/deptTree")
    public AjaxResult getDeptTree(){
        try {
            return AjaxResult.success(service.queryDeptTree());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}

